package com.example.hzq.zqb.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.EnterprisedetailActivity;
import com.example.hzq.zqb.bean.EnterpriseBean;

import java.util.List;

/**
 * Created by hzq on 2017/7/12.
 */

public class EnterpriseAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<EnterpriseBean> list;//保存要显示的数据
    public EnterpriseAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<EnterpriseBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView type,work,salary,address,time;
        public ViewHolder(View itemView) {
            super(itemView);
            type=(TextView)itemView.findViewById(R.id.e_item_type);
            work=(TextView)itemView.findViewById(R.id.workname);
            salary=(TextView)itemView.findViewById(R.id.salary);
            address=(TextView)itemView.findViewById(R.id.enterpriseaddress);
            time=(TextView)itemView.findViewById(R.id.enterprisetime);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.enterpriseitem, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final EnterpriseBean EnterpriseBean = list.get(position);
        if (EnterpriseBean == null)
            return;
        final ViewHolder viewholder = (ViewHolder) holder;
        viewholder.type.setText(EnterpriseBean.getType());
        GradientDrawable myGrad = (GradientDrawable)viewholder.type.getBackground();
        switch (position%4){
            case 0:
                myGrad.setColor(0xffffd500);
                break;
            case 1:
                myGrad.setColor(0xff21c687);
                break;
            case 2:
                myGrad.setColor(0xff007bff);
                break;
            case 3:
                myGrad.setColor(0xffe90400);
                break;
        }
        viewholder.work.setText(EnterpriseBean.getPosition());
        viewholder.salary.setText(EnterpriseBean.getSalary()+"元/小时");
        viewholder.address.setText("地址："+EnterpriseBean.getAddress());
        viewholder.time.setText("发布时间："+EnterpriseBean.getCreatetime());
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                Bundle bundle=new Bundle();
                intent.setClass(context,EnterprisedetailActivity.class);
                bundle.putSerializable("enterprise",EnterpriseBean);
               intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}
